﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using App.Models;
using App.Models.Pager;

namespace App.Controllers
{
    public class WakeHallInfoController : BaseController
    {                 
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult List(int? page, int? rows, string order)
        {
            dbe.ContextOptions.ProxyCreationEnabled = false;
            dbe.ContextOptions.LazyLoadingEnabled = false;
            PagedList<tb_WakeHallInfo> list = null;
            if ("asc" == order)
            {
                list = dbe.tb_WakeHallInfo.OrderBy(m => m.HallNO)
                                .ToPagedList(page ?? 1, rows ?? 10);
            }else{
                list = dbe.tb_WakeHallInfo.OrderByDescending(m => m.HallNO)
                                .ToPagedList(page ?? 1, rows ?? 10);
            }
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                tb_WakeHallInfo obj = new tb_WakeHallInfo{
                    HallNO = collection["HallNO"],
                    HallName = collection["HallName"],
                    Remark = collection["Remark"]
                };
                dbe.tb_WakeHallInfo.AddObject(obj);
                dbe.SaveChanges();
                var res = new { success = true };
                return Json(res, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                var res = new { success = false, msg = "保存出错！" };
                return Json(res, JsonRequestBehavior.AllowGet);
            }
        }
                        
        [HttpPost]
        public ActionResult Edit(string id, FormCollection collection)
        {
            try
            {
                string HallNO = collection["HallNO"];
                string HallName = collection["HallName"];
                string Remark = collection["Remark"];
                tb_WakeHallInfo obj = dbe.tb_WakeHallInfo.FirstOrDefault(m=>m.HallNO==HallNO);
                obj.HallName = HallName;
                obj.Remark = Remark;               
                dbe.SaveChanges();
                var res = new { success = true };
                return Json(res, JsonRequestBehavior.AllowGet);                
            }
            catch
            {
                var res = new { success = false, msg = "保存出错！" };
                return Json(res, JsonRequestBehavior.AllowGet);
            }
        }
        
        public ActionResult Delete(string ids)
        {
            try
            {
                string[] idList = ids.Split('&');
                foreach (var id in idList) {
                    var obj = dbe.tb_WakeHallInfo.FirstOrDefault(m => m.HallNO == id);
                    dbe.tb_WakeHallInfo.DeleteObject(obj);
                }
                
                dbe.SaveChanges();
                var res = new { success = true };
                return Json(res, JsonRequestBehavior.AllowGet);
            }
            catch {
                var res = new { success = false,msg="删除出错！" };
                return Json(res,JsonRequestBehavior.AllowGet);
            }
        }
    }
}
